// import { youdaoSynch } from '../translateService/youdaoSynch';

// import { Parser } from 'htmlparser2';
// import fs from 'fs';
// import path from 'path';
// const cacheFilePath = path.join(__dirname, 'cacheDataReadmeTranslate.json');
// console.log(cacheFilePath);
// let html = `
// <li><p>Do not expect your pull request to be reviewed immediately. Nix maintainers follow a
// <a href="https://github.com/NixOS/nix/tree/master/maintainers#project-board-protocol">structured process for reviews and design decisions</a>,
// which may or may not prioritise your work.</p>
// <p>Following this checklist will make the process smoother for everyone:</p>
// <ul>
// <li><input disabled="" type="checkbox"> Fixes an <a href="https://github.com/NixOS/nix/labels/idea%20approved">idea approved</a> issue</li>
// <li><input disabled="" type="checkbox"> Tests, as appropriate:<ul>
// <li>Functional tests – <a href="./tests/functional"><code>tests/functional/**.sh</code></a></li>
// <li>Unit tests – <a href="./src/"><code>src/*/tests</code></a></li>
// <li>Integration tests – <a href="./tests/nixos"><code>tests/nixos/*</code></a></li>
// </ul>
// </li>
// <li><input disabled="" type="checkbox"> User documentation in the <a href="./doc/manual/src">manual</a></li>
// <li><input disabled="" type="checkbox"> API documentation in header files</li>
// <li><input disabled="" type="checkbox"> Code and comments are self-explanatory</li>
// <li><input disabled="" type="checkbox"> Commit message explains <strong>why</strong> the change was made</li>
// <li><input disabled="" type="checkbox"> New feature or incompatible change:
// <a href="https://nix.dev/manual/nix/development/development/contributing.html#add-a-release-note">add a release note</a></li>
// </ul>
// </li>
// <li><p>If you need additional feedback or help to getting pull request into shape, ask other contributors using
// <a href="https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#mentioning-people-and-teams">@mentions</a>.</p>
// </li>
// </ol>
// <h2 id="making-changes-to-the-nix-manual">Making changes to the Nix manual</h2>
// <p>The Nix reference manual is hosted on <a href="https://nixos.org/manual/nix">https://nixos.org/manual/nix</a>. The underlying source files are located in
// <a href="./doc/manual/src"><code>doc/manual/src</code></a>. For small changes you can
// <a href="https://docs.github.com/en/repositories/working-with-files/managing-files/editing-files">use GitHub to edit these files</a>
// For larger changes see the <a href="https://nix.dev/manual/nix/development/development/contributing.html">Nix reference manual</a>.</p>
// <h2 id="getting-help">Getting help</h2>
// <p>Whenever you&#39;re stuck or do not know how to proceed, you can always ask for help. The appropriate channels to do so can
// be found on the <a href="https://nixos.org/community/">NixOS Community</a> page.</p>
// <p><code>please accept this pull Request</code> <code>please accept this pull Request</code></p>
// <pre><code class="language-python">def test():
//     print()
//     return 1

// print(test() )
// </code></pre>
// <ul>
// <li><input disabled="" type="checkbox"> <code>please accept this pull Request</code></li>
// <li>please accept this pull Request</li>
// </ul>
// <ul>
// <li>please accept this pull Request</li>
// </ul>
// <ul>
// <li>please accept this pull Request</li>
// </ul>
// <pre><code class="language-C++">//实现红黑树 和B树
// #include &lt;iostream&gt;
// using namespace std;

// int main() {
//     cout &lt;&lt; &quot;Hello, World!&quot; &lt;&lt; endl;
//     return 0;
// }
// </code></pre>

// `;
// import crpyto from 'crypto';
// //缓存查询内容
// import NodeCache from 'node-cache';
// const cache = new NodeCache();
// // 将缓存数据保存到文件
// function loadCacheFromFile() {
// 	if (fs.existsSync(cacheFilePath)) {
// 		const data = fs.readFileSync(cacheFilePath, 'utf-8');
// 		const cacheData = JSON.parse(data);
// 		for (const [key, value] of Object.entries(cacheData)) {
// 			cache.set(key, value);
// 		}
// 		console.log('Cache loaded from file');
// 	} else {
// 		console.log('No cache file found, starting with an empty cache');
// 	}
// }

// function saveCacheToFile() {
// 	const cacheData = cache.keys().reduce((obj, key) => {
// 		obj[key] = cache.get(key);
// 		return obj;
// 	}, {});
// 	fs.writeFileSync(cacheFilePath, JSON.stringify(cacheData, null, 2));
// 	console.log('Cache saved to file');
// }

// function getDataFromCache(key) {
// 	const data: any = cache.get(key) ?? null;
// 	if (data) {
// 		return data;
// 	}
// 	return data;
// }

// function setDataToCache(key, data) {
// 	console.log('Setting data to cache for key:', key);
// 	cache.set(key, data);
// 	saveCacheToFile();
// }

// function translateHtml(html: string) {
// 	let skip = false;
// 	let translatedHtml = `\n `;
// 	let translateDocument = ``;
// 	let htmlPreCode = `\n `;
// 	let docPosition = 0;
// 	loadCacheFromFile(); //从文件中加载缓存
// 	let parser = new Parser(
// 		{
// 			onopentag(name, attribs) {
// 				let attribString = '';
// 				for (const [key, value] of Object.entries(attribs)) {
// 					attribString += ` ${key}="${value}"`;
// 				}
// 				translatedHtml += `<${name}${attribString}>`;
// 				if (name === 'pre') {
// 					skip = true; // Set flag to skip content inside <pre> elements
// 				}
// 			},
// 			ontext(text) {
// 				if (!skip) {
// 					translateDocument += text + '@@';
// 					// result.push(translate(text))
// 					translatedHtml = translatedHtml + 'docPosition' + docPosition.toString();
// 					docPosition++;
// 				} else {
// 					htmlPreCode += text;
// 					translatedHtml += text;
// 				}
// 			},
// 			onclosetag(tagname) {
// 				translatedHtml += `</${tagname}>`;
// 				if (tagname === 'pre') {
// 					skip = false; // Reset flag when </pre> is encountered
// 				}
// 			},
// 		},
// 		{
// 			decodeEntities: true, // decode entities
// 		},
// 	);
// 	parser.write(html);
// 	parser.end();
// 	const transMap = new Map();
// 	let result: string[] = [];
// 	let hash = crpyto.createHash('md5').update(JSON.stringify(translateDocument)).digest('hex');
// 	if (getDataFromCache(hash) !== null) {
// 		result = getDataFromCache(hash);
// 	} else {
// 		result = youdaoSynch(translateDocument).split('@@');
// 		setDataToCache(hash, result);
// 	}
// 	console.log(docPosition);
// 	let resultDocument = `\n `;
// 	for (let i = 0; i <= docPosition; i++) {
// 		translatedHtml = translatedHtml.replace('docPosition' + i.toString(), result[i]); //replace是返回一个新字符串而不是修改原字符串
// 	}
// 	return translatedHtml;
// }

// import readline from 'readline';
// let rl = readline.createInterface({
// 	input: process.stdin,
// 	output: process.stdout,
// });
// rl.question('readline start ', (num) => {
// 	console.log(num);
// });

// rl.close(); // readLine 初始化之后不关闭会一直挂起
// let result = translateHtml(html);
let cheerio = require('cheerio');
// 示例HTML
export const html = `
  <div class="item">Item 1</div>
  <div class="item">Item 2 <pre><code>Code here</code></pre></div>
  <div class="item">Item 3</div>
  <pre><code>Code outside items</code></pre>
  <p>Paragraph 1</p>
  <p>Paragraph 2 <pre><code>Code inside paragraph</code></pre></p>
  <p>Paragraph 3</p>
`;

// 加载HTML
export const $ = cheerio.load(html);

// 初始化一个数组来保存内容
export let contents: any = [];

// 遍历所有非 <pre> 和 <code> 的元素
$('body')
	.children()
	.each((index, element) => {
		if (element.tagName !== 'pre' && element.tagName !== 'code') {
			// 克隆元素，移除 <pre><code> 标签，并获取文本内容
			const elementClone = $(element).clone();
			elementClone.find('pre, code').remove();
			let elementText: any = elementClone.text().trim();

			if (elementText) {
				contents.push(elementText);
			}
		}
	});

console.log(contents);
export default contents;
